package com.stockz.core.dao.hsql;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.stockz.core.dao.ExchangeDao;
import com.stockz.core.model.Exchange;

@Repository
@Transactional
public class ExchangeDaoImpl implements ExchangeDao {

	private EntityManager entityManager;
	
	@PersistenceContext
	public void setEntityManager(EntityManager entityManager) {
	    this.entityManager = entityManager;
	}
	
	@Override
	@Transactional(propagation=Propagation.MANDATORY)
	public void persist(Exchange exchange) {
		entityManager.persist(exchange);
	}

	@Override
	@Transactional(propagation=Propagation.MANDATORY, readOnly=true)
	public Exchange get(String exchangeCode) {
		return entityManager.find(Exchange.class, exchangeCode);
	}

	@Override
	@Transactional(propagation=Propagation.MANDATORY)
	public void persistOrUpdate(Exchange exchange) {
		Exchange e = get(exchange.getCode());
		if(e == null){
			persist(exchange);
		}else{
			e.setName(exchange.getName());
		}
	}
	
	@Override
	@Transactional(propagation=Propagation.MANDATORY)
	public void update(Exchange exchange){
		
	}

}
